<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="6">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          添加
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="18">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item label="字典码">
            <el-input
              v-model.trim="queryForm.condition.dictKey"
              clearable
              placeholder="请输入字典码"
            />
          </el-form-item>
          <el-form-item label="字典名称" label-width="80px">
            <el-input
              v-model.trim="queryForm.condition.dictDesc"
              clearable
              placeholder="请输入字典名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" label="编号" prop="id" width="100" />
      <el-table-column align="center" label="字典名称" prop="dictDesc" />
      <el-table-column align="center" label="字典码" prop="dictKey" />
      <el-table-column
        align="center"
        label="字典值"
        prop="dictValue"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="85"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <DictForm ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, getDictList } from '@/api/dict'
  import DictForm from './components/DictForm'

  export default {
    name: 'DictManagement',
    components: {
      DictForm,
    },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          condition: {
            dictKey: '',
            dictDesc: '',
          },
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            try {
              const res = await doDelete(row.id)
              if (res && res.status === 0) {
                this.$baseMessage(
                  '删除成功！',
                  'success',
                  'vab-hey-message-success'
                )
                await this.fetchData()
              } else {
                this.$baseMessage(
                  res ? res.msg : '删除失败！',
                  'error',
                  'vab-hey-message-error'
                )
              }
            } catch (e) {
              console.log(e)
              this.$baseMessage(
                e ? e.message : '删除失败！',
                'error',
                'vab-hey-message-error'
              )
            }
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        try {
          const res = await getDictList(this.queryForm)
          // console.log(res.data)
          if (res && res.data) {
            const { list, total } = res.data
            this.list = list
            this.total = total
          }
        } catch (e) {
          console.log(e)
        }
        this.listLoading = false
      },
    },
  }
</script>
